// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CloudQueueClient.cs" company="AzureContrib (Open source library http://azurecontrib.codeplex.com)">
//   Microsoft Public License (Ms-PL)
// </copyright>
// <summary>
//   Exception for errors when handling Windows Azure Cloud Drives.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;

using Microsoft.WindowsAzure.StorageClient;

namespace AzureContrib.WindowsAzure.StorageClient
{
	internal class CloudQueueClient : ICloudQueueClient
	{
		private readonly Microsoft.WindowsAzure.StorageClient.CloudQueueClient cloudQueueClient;

		internal CloudQueueClient(Uri baseAddressUri, IStorageCredentials credentials)
		{
			cloudQueueClient = new Microsoft.WindowsAzure.StorageClient.CloudQueueClient(baseAddressUri, credentials.Unwrap());
		}

		public event EventHandler<ResponseReceivedEventArgs> ResponseReceived
		{
			add { cloudQueueClient.ResponseReceived += value; }
			remove { cloudQueueClient.ResponseReceived -= value; }
		}

		public RetryPolicy RetryPolicy
		{
			get { return cloudQueueClient.RetryPolicy; }
			set { cloudQueueClient.RetryPolicy = value; }
		}

		public TimeSpan Timeout
		{
			get { return cloudQueueClient.Timeout; }
			set { cloudQueueClient.Timeout = value; }
		}

		public bool UsePathStyleUris
		{
			get { return cloudQueueClient.UsePathStyleUris; }
		}

		public IStorageCredentials Credentials
		{
			get { return cloudQueueClient.Credentials.Wrap(); }
		}

		public ICloudQueue GetQueueReference(string queueAddress)
		{
			return new CloudQueueWrapper(cloudQueueClient.GetQueueReference(queueAddress));
		}

		public IEnumerable<ICloudQueue> ListQueues()
		{
			return cloudQueueClient.ListQueues().Select(cloudQueue => new CloudQueueWrapper(cloudQueue)).Cast<ICloudQueue>();
		}

		public IEnumerable<ICloudQueue> ListQueues(string prefix)
		{
			return cloudQueueClient.ListQueues(prefix).Select(cloudQueue => new CloudQueueWrapper(cloudQueue)).Cast<ICloudQueue>();
		}

		public IEnumerable<ICloudQueue> ListQueues(string prefix, QueueListingDetails detailsIncluded)
		{
			return cloudQueueClient.ListQueues(prefix, detailsIncluded).Select(cloudQueue => new CloudQueueWrapper(cloudQueue)).Cast<ICloudQueue>();
		}

		public IResultSegment<ICloudQueue> ListQueuesSegmented()
		{
			return new ResultSegmentWrapper<CloudQueue, ICloudQueue>(cloudQueueClient.ListQueuesSegmented(), CloudQueueWrapper.Convert);
		}

		public IResultSegment<ICloudQueue> ListQueuesSegmented(string prefix, QueueListingDetails detailsIncluded)
		{
			return new ResultSegmentWrapper<CloudQueue, ICloudQueue>(cloudQueueClient.ListQueuesSegmented(prefix, detailsIncluded), CloudQueueWrapper.Convert);
		}

		public IResultSegment<ICloudQueue> ListQueuesSegmented(string prefix, QueueListingDetails detailsIncluded, int maxResults, ResultContinuation continuationToken)
		{
			return new ResultSegmentWrapper<CloudQueue, ICloudQueue>(cloudQueueClient.ListQueuesSegmented(prefix, detailsIncluded, maxResults, continuationToken), CloudQueueWrapper.Convert);
		}

		public IAsyncResult BeginListQueuesSegmented(AsyncCallback callback, object state)
		{
			return cloudQueueClient.BeginListQueuesSegmented(callback, state);
		}

		public IAsyncResult BeginListQueuesSegmented(string prefix, AsyncCallback callback, object state)
		{
			return cloudQueueClient.BeginListQueuesSegmented(prefix, callback, state);
		}

		public IAsyncResult BeginListQueuesSegmented(string prefix, QueueListingDetails detailsIncluded, AsyncCallback callback, object state)
		{
			return cloudQueueClient.BeginListQueuesSegmented(prefix, detailsIncluded, callback, state);
		}

		public IAsyncResult BeginListQueuesSegmented(string prefix, QueueListingDetails detailsIncluded, int maxResults, ResultContinuation continuationToken, AsyncCallback callback, object state)
		{
			return cloudQueueClient.BeginListQueuesSegmented(prefix, detailsIncluded, maxResults, continuationToken, callback, state);
		}

		public IResultSegment<ICloudQueue> EndListQueuesSegmented(IAsyncResult asyncResult)
		{
			return new ResultSegmentWrapper<CloudQueue, ICloudQueue>(cloudQueueClient.EndListQueuesSegmented(asyncResult), CloudQueueWrapper.Convert);
		}
	}
}